/*
 * GridPanel.hpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   GridPanel.hpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   3 sep 2012
 */

#ifndef GRIDPANEL_HPP
#define GRIDPANEL_HPP

#include <wx/wx.h>
#include <wx/grid.h>
#include <boost/shared_ptr.hpp>

#include "ThermistorTable.hpp"

#include <map>
#include <stdexcept>

using boost::shared_ptr;

namespace tltgen {

  class Grid;
  class CircuitDialog;
  class ThermistorParamsPanel;
  class TemperatureScale;
  class GridPanel : public wxPanel {
  public:
    GridPanel(wxWindow* parent, wxWindowID id, const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<LeastSquaresSolver>& leastSquaresSolver, const shared_ptr<TemperatureScale>& temperatureScale, const wxPoint& pos, const wxSize& size);
    virtual ~GridPanel();

    void initializeComponents(const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<LeastSquaresSolver>& leastSquaresSolver, const shared_ptr<TemperatureScale>& temperatureScale);

    static bool isValidThermistorTable(const shared_ptr<ThermistorTable>& thermistorTable);
    shared_ptr<ThermistorTable> getThermistorTable() const;

    static bool isValidLeastSquaresSolver(const shared_ptr<LeastSquaresSolver>& leastSquaresSolver);
    shared_ptr<LeastSquaresSolver> getLeastSquaresSolver() const;

    static bool isValidTemperatureScale(const shared_ptr<TemperatureScale>& temperatureScale);
    void setTemperatureScale(const shared_ptr<TemperatureScale>& temperatureScale);
    shared_ptr<TemperatureScale> getTemperatureScale() const;

    void addThermistorModel(const shared_ptr<ThermistorModel>& thermistorModel);

  protected:
    DECLARE_EVENT_TABLE();

    void onThermistorTableRowChange(ThermistorTableRow::Event e, ThermistorTableRow *tableRow, ThermistorTableRow::Column col);

    bool isEmptyGridRow(int row) const;
    double getCellValueAsDouble(int row, int col) const;
    int getCellValueAsPositiveInt(int row, int col) const;
    void clearCellValue(int row, int col);

    bool isValidGridRowTemperature(int gridRow) const;
    Temperature getGridRowTemperature(int gridRow) const;
    bool isValidGridRowResistance(int gridRow) const;
    Resistance getGridRowResistance(int gridRow) const;
    bool isValidGridRowVoltage(int gridRow) const;
    Voltage getGridRowVoltage(int gridRow) const;
    bool isValidGridRowAdcValue(int gridRow) const;
    int getGridRowAdcValue(int gridRow) const;

    wxString formatTemperature(Temperature t) const;
    wxString formatResistance(Resistance r) const;
    wxString formatVoltage(Voltage v) const;
    wxString formatAdcValue(int a) const;

    void updateGridRowColumn(ThermistorTableRow::Column col, int gridRow, ThermistorTableRow *tableRow);
    void updateGridRowTemperature(int gridRow, ThermistorTableRow *tableRow);
    void updateGridRowResistance(int gridRow, ThermistorTableRow *tableRow);
    void updateGridRowVoltage(int gridRow, ThermistorTableRow *tableRow);
    void updateGridRowAdcValue(int gridRow, ThermistorTableRow *tableRow);
    void updateGridRow(int gridRow, ThermistorTableRow *tableRow);

    ThermistorTableRow::Column mapColumn(int col) const;

    void onGridCellChange(wxGridEvent& event);
    void onGridLabelRightClick(wxGridEvent& event);
    void onRemoveRowClick(wxCommandEvent& event);
    void onCircuitSettingsButtonClicked(wxCommandEvent& event);

    void updateThermistorTableRow(ThermistorTableRow* tableRow, int gridRow, int gridCol);
    void checkRemoveGridRow(int gridRow);
    void removeGridRow(int gridRow, ThermistorTableRow* tableRow);
    void ensureEmptyBottomGridRow();

    bool hasGridToTableMapping(int gridRow) const;
    bool hasTableToGridMapping(ThermistorTableRow* tableRow) const;
    ThermistorTableRow *getGridToTableMapping(int gridRow);
    int getTableToGridMapping(ThermistorTableRow* tableRow);
    void addGridToTableMapping(int gridRow, ThermistorTableRow *tableRow);
    //void removeGridToTableMapping(int gridRow);

    
    shared_ptr<TemperatureScale> temperatureScale;
    shared_ptr<ThermistorTable> thermistorTable;
    std::map<int, ThermistorTableRow*> gridToTableMap;
    std::map<ThermistorTableRow*, int> tableToGridMap;

    int lastGridHeaderClicked;
    wxMenu* rowMenu;
    Grid* grid;
    ThermistorParamsPanel* thermistorParamsPanel;
    CircuitDialog* circuitDialog;
  };

  enum {
    ID_Grid = 1,
    ID_CircuitSettingsButton,
    ID_MenuRemoveRow,
  };

}

#endif


